package com.example.qiandao.repository;

import com.example.qiandao.entity.Clazz;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface ClazzRepository extends JpaRepository<Clazz, Long> {

    // 根据课程ID查询其所有班级
    @Query("SELECT c FROM Clazz c WHERE c.course.id = :courseId")
    List<Clazz> findClazzesByCourseId(@Param("courseId") Long courseId);

    @Query("SELECT c FROM Clazz c JOIN c.students s WHERE s.id = :studentId")
    List<Clazz> findClassesByStudentId(@Param("studentId") Long studentId);

    /**
     * 根据学生ID和课程ID查询学生在此课程下加入的所有班级
     *
     * @param studentId 学生ID
     * @param courseId 课程ID
     * @return 学生在此课程下加入的所有班级
     */
    @Query("SELECT c FROM Clazz c JOIN c.students s WHERE s.id = :studentId AND c.course.id = :courseId")
    List<Clazz> findClassesByStudentIdAndCourseId(@Param("studentId") Long studentId, @Param("courseId") Long courseId);


    /**
     * 根据班级码查找班级
     *
     * @param classCode 班级码
     * @return 班级对象
     */
    Clazz findByClassCode(String classCode);
}